//
//  BCProjectIntroductionRiskLevelCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/23.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectIntroductionRiskLevelCell.h"
//
#import "UICollectionViewLeftAlignedLayout.h"

@interface BCProjectIntroductionRiskLevelSubCell : UICollectionViewCell

/**
 *levelLabel
 */
@property (nonatomic, strong) QMUILabel *levelLabel;

@end

@implementation BCProjectIntroductionRiskLevelSubCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.levelLabel = [QMUILabel new];
        self.levelLabel.textAlignment = NSTextAlignmentCenter;
        self.levelLabel.textColor = [UIColor qmui_colorWithHexString:@"#ADADAD"];
        self.levelLabel.adjustsFontSizeToFitWidth = true;
        self.levelLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
        self.levelLabel.contentEdgeInsets = UIEdgeInsetsMake(0, 1, 0, 1);
        [self addSubview:self.levelLabel];
        [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
    }
    return self;
}

@end


@interface BCProjectIntroductionRiskLevelCell () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout>

@property (nonatomic, strong) QMUILabel *riskLabel;

@property (nonatomic, strong) UICollectionView *levelCollection;

@end

@implementation BCProjectIntroductionRiskLevelCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.riskLabel = [QMUILabel new];
    self.riskLabel.textColor = [UIColor whiteColor];
    self.riskLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightRegular];
    self.riskLabel.adjustsFontSizeToFitWidth = true;
    self.riskLabel.numberOfLines = 2;
    self.riskLabel.textAlignment = NSTextAlignmentCenter;
    self.riskLabel.contentEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 2);
    [self addSubview:self.riskLabel];
    [self.riskLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.mas_equalTo(self);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(self).multipliedBy(.5);
    }];
    
    UICollectionViewLeftAlignedLayout *leftLayout = [UICollectionViewLeftAlignedLayout new];
    leftLayout.sectionInset = UIEdgeInsetsZero;
    leftLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    leftLayout.minimumLineSpacing = 0;
    leftLayout.minimumInteritemSpacing = 0;
    self.levelCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:leftLayout];
    self.levelCollection.backgroundColor = [UIColor clearColor];
    self.levelCollection.userInteractionEnabled = false;
    self.levelCollection.dataSource = self;
    self.levelCollection.delegate = self;
    [self addSubview:self.levelCollection];
    [self.levelCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.height.mas_equalTo(self).multipliedBy(.5);
    }];
    //
    [self.levelCollection registerClass:BCProjectIntroductionRiskLevelSubCell.class forCellWithReuseIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelSubCell.class)];
}
#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.riskLevel.commonRiskSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCProjectIntroductionRiskLevelModel *model = self.riskLevel.commonRiskSource[indexPath.row];
    //
    BCProjectIntroductionRiskLevelSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelSubCell.class) forIndexPath:indexPath];
    cell.levelLabel.text = model.level;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat collectionWidth = collectionView.frame.size.width;
    CGFloat collectionHeight = collectionView.frame.size.height;
    CGFloat width = collectionWidth;
    if (self.riskLevel.commonRiskSource.count) {
        width = collectionWidth/self.riskLevel.commonRiskSource.count;
    }
    //
    return CGSizeMake(width, collectionHeight);
}



- (void)setRiskLevel:(BCProjectIntroductionRiskLevelModel *)riskLevel {
    _riskLevel = riskLevel;
    //
    self.riskLabel.text = riskLevel.risk;
    
    @try {
        self.riskLabel.backgroundColor = [UIColor qmui_colorWithHexString:riskLevel.color];
    } @catch (NSException *exception) {
        NSLog(@"%@",exception);
        self.riskLabel.backgroundColor = [UIColor qmui_randomColor];
    } @finally {
    }
}


+ (CGFloat)defaultHeight {
    return 36.0;
}

@end
